<template>
  <div class="tp-box">
    <a-drawer
      :visible="props.visible"
      :width="props.width"
      :closable="props.closable"
      :destroyOnClose="props.destroyOnClose"
      @close="handleClose"
    >
      <template #title>
        <div class="title-detail">
          <Tooltip placement="top">
            <template #title>
              <span>{{ getTipTitle }}</span>
            </template>
            <h3 style="padding-top: 8px;">{{ getTitle }}</h3>
          </Tooltip>
          <div style="width: 400px; padding-top: 5px">
            <a-input-search
              v-model:value="txtKeyWord"
              placeholder="请输入核查人员姓名进行查询"
              enter-button
              style="width: 100%"
              @search="searchBtn"
            />
          </div>
        </div>
      </template>

      <!-- :scroll="{ x: 1500, y: 600 }" -->
      <a-table
        :columns="computedTableColumn"
        :data-source="computedCheckTableDataList"
        :page-size-options="pageSizeOptions"
        :loading="loading"
        bordered
        :pagination="false"
        size="middle"
        :scroll="{ x: 'calc(700px + 50%)', y: props.detailTableHeight }"
      >
        <!-- <template #action>
          <a>action</a>
        </template> -->
      </a-table>
      <div class="pagination">
        <a-pagination
          show-size-changer
          v-model:current="pageIndex"
          v-model:pageSize="pageSize"
          :total="total"
          @show-size-change="onShowSizeChange"
        />
      </div>
    </a-drawer>
    <VillageDetail
      v-model:vvvisible="uservisible"
      title="抽屉标题"
      :width="1500"
      :detailTableHeight="detailTableHeight"
      :vclosable="true"
      :vdestroyOnClose="true"
      :data="vvvvdetailData"
      :vvvvvdetailParams="vvvvdetailParams"
      :vvvvcurrentTitle="vvvv_currentTitle"
      @close="handleVillageDetailClose"
    >
      <!-- <p>抽屉内容</p> -->
    </VillageDetail>
  </div>
</template>
<script setup lang="ts">
  import { Tooltip } from 'ant-design-vue';
  import { ref, computed, onMounted, watch } from 'vue';
  import { getXzqByCheckUserList } from '/@/api/demo/system';
  import VillageDetail from '../component/detailDrawer.vue';
  // 表格的 ref Dom名称
  const uservisible = ref(false);
  let vvvvdetailData = ref<any[]>([]);
  let vvvvdetailParams = ref({}); // 详情参数
  let detailTableHeight = ref<number>(600); // 表格高度
  let vvvv_currentTitle = ref({});
  const txtKeyWord = ref<string>();
  // 默认传过来的值q
  const props = defineProps({
    visible: {
      type: Boolean,
      default: true,
    },
    title: {
      type: String,
      default: '',
    },
    width: {
      type: Number,
      default: 256,
    },
    detailTableHeight: {
      type: Number,
      default: 600,
    },
    hzfs: {
      type: String,
      default: '',
    },
    closable: {
      type: Boolean,
      default: true,
    },
    data: {
      type: Array,
      default: () => [],
    },
    detailParams: {
      type: Object,
      default: () => {},
    },
    currentXzqdmname: {
      type: String,
      default: '',
    },
    currentTitle: {
      type: String,
      default: '',
    },
    wscolsList: {
      type: Array,
      default: () => [{ wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }],
    },
    destroyOnClose: {
      type: Boolean,
      default: true,
    },
    onClose: {
      type: Function as PropType<() => void>,
      default: () => {},
    },
  });
  let pageIndex = ref(1);
  let pageSize = ref(15);
  let loading = ref(false);
  const pageSizeOptions = ref<string[]>(['100', '200', '300']);
  let CheckTableSource = ref<any>([]);
  let total = ref(0);
  const columns = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
      width: 80,
    },
    {
      title: '企安安ID',
      dataIndex: 'qaakey',
      key: 'qaakey',
      align: 'center',
      width: 100,
    },
    {
      title: '用户姓名',
      dataIndex: 'nickname',
      key: 'nickname',
      align: 'center',
      width: 80,
    },
    {
      title: '绑定时间',
      dataIndex: 'create_on',
      key: 'create_on',
      align: 'center',
      width: 120,
    },
    {
      title: '用户类型',
      dataIndex: 'role_type',
      key: 'role_type',
      align: 'center',
      width: 80,
    },
    {
      title: '核查房屋栋数',
      dataIndex: 'housecount',
      key: 'housecount',
      align: 'center',
      width: 80,
      customCell: (row, column) => {
        let style = 'cursor:pointer;text-decoration: underline; color: blue;';
        let isNoZero = row[column.key] !== 0 ? true : false;
        return {
          style: style,
          class: 'cell-hover',
          onClick: async () => {
            getDetailData(row, isNoZero, '村级用户');
          },
        };
      },
    },
  ];
  // 计算属性，用于实时更新数据到页面上
  const computedTableColumn = computed(() => {
    let newColumn: any = [];
    newColumn = columns;
    return newColumn;
  });
  const computedCheckTableDataList = computed(() => {
    return CheckTableSource.value;
  });
  const getTitle = computed(() => {
    // let str = `【${props.currentXzqdmname} 】-- props.detailParams.titlename
    console.log('******参数*33333*****', props);
    let str = `【${props.currentTitle}】核查人员`;
    if (str.length > 50) {
      str = str.substring(0, 50) + '...';
    }
    return str;
  });
  const getTipTitle = computed(() => {
    let str = `${props.currentTitle}`;
    return str;
  });
  watch(
    () => props.visible,
    (newVal) => {
      // console.log(`visible 变化了，新值：${newVal}，旧值：${oldVal}`);
      // 在这里执行你需要的操作
      if (newVal) {
        getRecordTableData();
      }
    },
  );
  const getRecordTableData = async () => {
    let params = {
      pageIndex: pageIndex.value,
      pageSize: pageSize.value,
      txtKeyWord: txtKeyWord.value,
      xzq: props.detailParams.xzq,
      category: props.detailParams.category,
    };
    loading.value = true;
    let res: any = await getXzqByCheckUserList(params);

    loading.value = false;
    let list = res.items;
    console.log('********res*************', res);
    CheckTableSource.value = list;
    total.value = res.total;
  };
  //  调用 父组件传过来的方法
  const emit = defineEmits(['close']);

  const getDetailData = async (row: any, isNoHzyh: boolean, category: string) => {
    if (isNoHzyh) {
      let params: any = {
        xzq: props.detailParams.xzq,
        category: props.detailParams.category,
        qaakey: row.qaakey,
        ...props.detailParams,
      };
      vvvvdetailParams.value = params;
      vvvv_currentTitle.value = row.xzq + '-' + category + '-' + row.qaakey;
      showDeatailDrawer();
    }
  };
  function searchBtn() {
    loading.value = true;
    getRecordTableData();
  }
  // 打开抽屉
  function showDeatailDrawer() {
    uservisible.value = true;
  }
  const handleClose = () => {
    emit('close');
    pageIndex.value = 1;
    pageSize.value = 15;
    uservisible.value = false;
  };
  const handleVillageDetailClose = () => {
    uservisible.value = false;
  };
  // 切换页码的时候更新  pageIndex 和  pageSize
  const onShowSizeChange = () => {
    // antDesign  自动给  pageIndex 和 pageSize 赋值
    // pageIndex.value = pageIndex;
    // pageSize.value = pageSize;
    // console.log('@pageIndex,pageSize', pageIndex, pageSize);
  };
  watch(pageSize, () => {
    // console.log('pageSize', pageSize.value);
    getRecordTableData();
  });
  watch(pageIndex, () => {
    // console.log('current', pageIndex.value);
    getRecordTableData();
  });

</script>
<style lang="less" scoped>
  .tp-box {
    height: 100%;
  }
  .title-detail {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .pagination {
    margin-top: 5px;
    display: flex;
    justify-content: flex-end;
  }
</style>
./detailDrawer.vue